package com.lagou.educommentboot.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lagou.eduapi.entity.CourseComment;
import com.lagou.eduapi.entity.CourseCommentFavoriteRecord;
import com.lagou.educommentboot.mapper.CourseCommentDao;
import com.lagou.educommentboot.mapper.CourseCommentFavoriteRecordDao;
import com.lagou.educommentboot.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CourseCommentDao courseCommentDao;

    @Autowired
    private CourseCommentFavoriteRecordDao courseCommentFavoriteRecordDao;

    public Integer saveComment(CourseComment comment) {
        return courseCommentDao.insert(comment);
    }

    public List<CourseComment> getCommentsByCourseId(Integer courseId, Integer offset, Integer pageSize) {
        return courseCommentDao.getCommentsByCourseId(courseId, offset, pageSize);
    }

    /*点赞：
    * 先查看当前用户是否对这条留言点过赞
    * 如果点过：修改is_del
    * 如果没点过：新增一条点赞信息
    */
    public Integer saveFavorite(Integer commentId, Integer userId) {
//
        QueryWrapper<CourseCommentFavoriteRecord> qw = new QueryWrapper<>();
        qw.eq("comment_id", commentId);
        qw.eq("user_id", userId);
        Integer i = courseCommentFavoriteRecordDao.selectCount(qw);
        int i1 = 0;
        int i2 = 0;
        if (i == 0){
//            i1 = courseCommentDao.saveCommentFavorite(commentId, userId);
            CourseCommentFavoriteRecord courseCommentFavoriteRecord = new CourseCommentFavoriteRecord();
            courseCommentFavoriteRecord.setCommentId(commentId);
            courseCommentFavoriteRecord.setUserId(userId);
            courseCommentFavoriteRecord.setIsDel(0);
            courseCommentFavoriteRecord.setCreateTime(new Date());
            courseCommentFavoriteRecord.setUpdateTime(new Date());
            i1 = courseCommentFavoriteRecordDao.insert(courseCommentFavoriteRecord);
        }else {
//            i1 = courseCommentDao.updateFavoriteStatus(commentId, 0,userId);
            CourseCommentFavoriteRecord courseCommentFavoriteRecord = new CourseCommentFavoriteRecord();
            courseCommentFavoriteRecord.setIsDel(0);
            QueryWrapper<CourseCommentFavoriteRecord> qw1 = new QueryWrapper<>();
            qw1.eq("comment_id", commentId);
            qw1.eq("user_id", userId);
            i1 = courseCommentFavoriteRecordDao.update(courseCommentFavoriteRecord,qw1);
        }

        i2 = courseCommentDao.updateLikeCount(1,commentId);

        if (i1==0 || i2 == 0){
            throw new RuntimeException("点赞失败！");
        }
        return commentId;
    }

    /*取消点赞的信息
    * 更新留言赞的数量-1
    * */
    public Integer cancelFavorite(Integer commentId, Integer userId) {
//        Integer i1 = courseCommentDao.updateFavoriteStatus(commentId,1, userId);
        CourseCommentFavoriteRecord courseCommentFavoriteRecord = new CourseCommentFavoriteRecord();
        courseCommentFavoriteRecord.setIsDel(1);
        QueryWrapper<CourseCommentFavoriteRecord> qw = new QueryWrapper<>();
        qw.eq("comment_id", commentId);
        qw.eq("user_id", userId);
        Integer i1 = courseCommentFavoriteRecordDao.update(courseCommentFavoriteRecord,qw);
        Integer i2 = courseCommentDao.updateLikeCount(-1, commentId);
        if (i1==0 || i2==0){
            throw new RuntimeException("取消赞失败！");
        }
        return i2;
    }

}
